// -------------------------------------------
//					Line
// -------------------------------------------

#include "Line.h"
#include <iostream>

using namespace std;

Line::Line(const string& line)
{
    string tempWord = "";
    size_t posOfSpace = 0;
    size_t prevPosOfSpace = 0;

    //cout <<  line << endl;

    for ( int i=0 ; i < line.size() ; i++ )
    {

        posOfSpace = line.find(" ", posOfSpace+1);

        if (posOfSpace != string::npos)
        {
        int wordLength=0;
        wordLength = posOfSpace - prevPosOfSpace;
        tempWord = line.substr(prevPosOfSpace, wordLength );
        prevPosOfSpace = posOfSpace+1;
        //tempString = tempString[posOfSpace, line.size()];
        Word foundWord(tempWord);
        //cout << tempWord << endl;
        //foundWord.display();
        _line.push_back(foundWord);
        tempWord.clear();

        }

        if (posOfSpace == string::npos)
        {
            int wordLength=0;
            wordLength = posOfSpace - prevPosOfSpace;
            posOfSpace = line.size() -1;
            tempWord = line.substr(prevPosOfSpace, wordLength);
            Word lastWord(tempWord);
            //lastWord.display();
            _line.push_back(lastWord);
            i = line.size() + 5;
        }

    }
    //cout << _line.size() << endl;
}

bool Line::contains(const Word& search_word) const
{
    //cout << "line size: " << _line.size() << endl;
    if (_line.empty())
    {
        //cout << "Empty line" << endl;
        return false;
    }

    if (!search_word.isQueryable())
    {
        //cout << "Word is not queryable" << endl;
        return false;
    }

    else
    {
        bool notFound = true;
        for ( int i=0; i<_line.size(); i++)
        {
            if (search_word == _line[i])
            {
                //cout << "Word found" << endl;
                i = _line.size() + 1;
                notFound = false;
                return true;
            }
        }

        if (notFound ==true)
        {
            //cout << "Word not found" << endl;
            return false;
        }
    }

}
